import React,{Component} from 'react';
import List from '../../newslist/List';
import Loading from '../../../component/loading/Loading';
import ajax from '../../../component/httprequest/Request';
import url from '../../../component/posturl/BaseUrl';
import qs from 'qs';
import PropTypes from 'prop-types';
const prefix = 'dynamic';
class CommonList extends Component{
    constructor(props){
        super(props)
        this.state = {
            listSource:[],
            display:"none"
        }
    };
    loading = () =>{
        if(this._isMounted){
            this.setState({
                display:"block"
            })
        }
    };
    componentWillMount(){
        this._isMounted = true;
        let category = this.props.category;
        let getNewsListUrl = `${url}/official/getArticle`;
        let data = qs.stringify({"id":"", "category":category});
        ajax.ajaxRequest(undefined,getNewsListUrl,data,this.loading)
            .then(res=>{
                console.log(res)
                if(this._isMounted){
                    this.setState({
                        listSource:res.data,
                        display:"none"
                    })
                }
            })
            .catch(err=>{
                console.log(err)
            })
    };
    componentWillUnmount (){
        this._isMounted = false;
    };
    render() {
        let state = this.state;
        let {contentStyle,isReferral,category} = this.props;
        return (
            <div className={prefix}>
                <Loading className={prefix+'-loading'} style={{display:state.display}}/>
                <List listSource={state.listSource} contentStyle={contentStyle} isReferral={isReferral} category={category}/>
            </div>
        )
    }
};
export default CommonList;
CommonList.propTypes = {
    category:PropTypes.number.isRequired,//文章分类
    contentStyle:PropTypes.object, //文章列表内容区域样式
    isReferral:PropTypes.oneOf([true,false]).isRequired//是否属于梅奥转诊分类下的界面
}